package com.renchi.menu.Leaving.service;

import com.renchi.menu.Leaving.dao.LeavDbDao;
import com.renchi.menu.Leaving.entity.LeavDbEntity;
import com.renchi.menu.Leaving.vo.LeavDbVo;
import com.renchi.menu.common.page.FastPage;
import com.renchi.menu.common.page.PageInfo;
import com.renchi.menu.common.result.Result;
import com.renchi.menu.common.result.ResultModel;
import com.renchi.menu.common.result.ResultTools;
import com.renchi.menu.common.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class LeavDbService {
    @Autowired
    private LeavDbDao leavDbDao;

    public Result saveLeavDb(LeavDbEntity leavDbEntity){
        try {
            Date nowTime = new Date();
            java.sql.Date time = new java.sql.Date(nowTime.getTime());
            String uuid= UUID.randomUUID().toString().replace("-", "").toLowerCase();
            leavDbEntity.setLeav_id(uuid);
            leavDbEntity.setCreate_time(time);
            leavDbEntity.setUpdate_time(time);
            leavDbDao.save(leavDbEntity);
            return ResultTools.result(0,"",null);
        }catch (Exception e){
            return ResultTools.result(404,e.getMessage(),null);
        }
    }

    public ResultModel<PageInfo<LeavDbVo>> findLeavDb(LeavDbVo leavDbVo){
        List<LeavDbEntity> leavDbEntities= leavDbDao.findAllById(leavDbVo.getMenu_url());
        List<LeavDbVo> leavDbVos= BeanConvertUtils.convertVoCollection(leavDbEntities,LeavDbVo.class);

        //分页
        PageInfo<LeavDbVo> pageInfo = FastPage.getListVoPageInfo(leavDbVos,leavDbVo.getPageSize(),leavDbVo.getPage());
        return ResultModel.of(pageInfo);
    }
}
